﻿unit VCL1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, ToolWin, R4FuncKeys, Grids, R4Grid, Buttons,
  FileInfo,LoadFiles,Search,SearchFiles,CSVFile,EMMemList,CSVEXForm;

type

  TForm1 = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    CheckBox1: TCheckBox;
    find: TEdit;
    R4Funckeys1: TR4Funckeys;
    R4Grid1: TR4Grid;
    done: TR4FuncBtn;
    R4FuncBtn2: TR4FuncBtn;
    CSVImport: TR4FuncBtn;
    R4FuncBtn4: TR4FuncBtn;
    Next: TR4FuncBtn;
    R4FuncBtn6: TR4FuncBtn;
    R4FuncBtn7: TR4FuncBtn;
    R4FuncBtn8: TR4FuncBtn;
    R4FuncBtn9: TR4FuncBtn;
    Search: TR4FuncBtn;
    Withdraw: TR4FuncBtn;
    R4FuncBtn12: TR4FuncBtn;
    R4FuncBtn13: TR4FuncBtn;
    CSVOutput: TR4FuncBtn;
    R4FuncBtn15: TR4FuncBtn;
    R4FuncBtn16: TR4FuncBtn;
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure CSVImportClick(Sender: TObject);
    procedure R4Grid1CellData(ACol, ARow: Integer; var varValue: Variant);
    procedure WithdrawClick(Sender: TObject);
    procedure doneClick(Sender: TObject);
    procedure R4Grid1TitleClick(ACol: Integer);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure SearchClick(Sender: TObject);
    procedure SearchResultSubmit(Sender:TObject;SN :string);
    procedure NextClick(Sender: TObject);
    procedure CSVOutputClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }

  end;

var
  Form1: TForm1;
  Form2: TForm2;
  Form3: TForm3;
  //FileList : TList;
  FileList : TEMMemList;
  MyFileInfo : PFileInfo;
  I : Integer;
  IsUpload : Integer=0;
  IsSorted : Array[1..5] of Integer;
  FilePath : string;
  SName : string;
  ResultList : TStringList;



implementation

{$R *.dfm}

procedure TForm1.CSVImportClick(Sender: TObject);
var
  CSVInPath : string;
begin
  CSVInPath := find.Text;
  //CSVInPath := 'D:\test\CSVResultFile.txt';
  if(Length(CSVInPath)=0) then
  begin
    showmessage('The path is empty,please enter the path.');
    exit;
  end;
  if (not FileExists(CSVInPath)) then
  begin
    showmessage('The file is not exists,please re-enter.');
    exit;
  end;
  LoadCSV(FileList,CSVInPath);
  R4Grid1.RowCount :=  FileList.Count + 1;
  R4Grid1.Refresh;
end;

//检索结果CSV导出
procedure TForm1.CSVOutputClick(Sender: TObject);
var
  ExportPath : string;
begin
  if(FileList.Count = 0) then
  begin
    showmessage('The search result is empty.');
    exit;
  end;

  Form3 := TForm3.Create(Self); //将源窗口作为父窗口
  try
    Form3.position := poMainFormCenter;
    if(Form3.ShowModal = mrOk) then
    begin
      ExportPath := concat(Form3.CSVEXPath.Text,'\CSVResultFile.txt');
      ExportToCSV(FileList,ExportPath);
      showmessage('Successfully.The filepath is ' + ExportPath);
      exit;
    end;
  except
    Form3.Free;
    raise;
  end;
end;

procedure TForm1.doneClick(Sender: TObject);
begin
  Close;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
    FileList := TEMMemList.Create(30);
    ResultList := TStringList.Create;
    R4Grid1.Columns[1].Title.Caption := 'ファイル名';
    R4Grid1.Columns[2].Title.Caption := '路径';
    R4Grid1.Columns[3].Title.Caption := '類型';
    R4Grid1.Columns[4].Title.Caption := '大小';
    R4Grid1.Columns[5].Title.Caption := '日期';
    for I := 0 to R4Grid1.Columns.Count - 1 do
    begin
      R4Grid1.Columns[I].Title.Alignment := taCenter;
    end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
//  for I := 0 to FileList.Count - 1 do
//  begin
//    dispose(FileList[I]);
//  end;
  FileList.Free;
  ResultList.Free;
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  R4Funckeys1.FuncKey(Key);
end;

procedure TForm1.NextClick(Sender: TObject);
var
  Row : Integer;
  Index : Integer;
begin
  if Length(SName)<=0 then
  begin
    showmessage('Search content is empty.');
    exit;
  end;
  if ResultList.Count = 0 then
  begin
    showmessage('検索条件に一致するデータは見つかりません。');
    exit;
  end;
  //结果list为1，说明精确查找或模糊查找只有一个符合条件
  if ResultList.Count = 1 then
  begin
    Row := SecondSearch(ResultList,FileList,SName); //为1，可能是精确查找的结果，所以要模糊查找一次
    if(Row=1) then   // 长度为1，说明只有当前这条符合条件
    begin
      R4Grid1.Row := StrToInt(ResultList.Names[0])+1;
      showmessage('It is the last one');
      exit;
    end;
  end;
  //结果List大于1，循环显示结果
  if ResultList.Count > 1 then
  begin
    Row := ResultList.IndexOfName(IntToStr(R4Grid1.Row-1));    //查找当前选中行的下标
    if(Row = ResultList.Count - 1) then      //如果当前行是resultlist最后一个元素，则选中list的第一个元素,否则下一个
    begin
      R4Grid1.Row := StrToInt(ResultList.Names[0])+1;
      exit;
    end
    else
    begin
      R4Grid1.Row := StrToInt(ResultList.Names[Row+1])+1;
    end;
    exit;
  end;
end;

//显示数据
procedure TForm1.R4Grid1CellData(ACol, ARow: Integer; var varValue: Variant);
begin
  if(ARow >= 1) and (ARow <= FileList.Count) then
  begin
    MyFileInfo := FileList[ARow - 1];
    case ACol of
    1:
      varValue := MyFileInfo.FileName;
    2:
      varValue := MyFileInfo.FilePath;
    3:
      varValue := MyFileInfo.FileType;
    4:
      varValue := FloatToStr(MyFileInfo.FileSize) + 'KB';
    5:
      varValue := MyFileInfo.FileDate;
    end;
  end;
end;

//点击表头进行排序
procedure TForm1.R4Grid1TitleClick(ACol: Integer);
begin
    if(IsSorted[Acol] = 0) then
    begin
      Sorts(FileList,Acol,IsSorted[Acol]);
      IsSorted[Acol] := 1;
    end
    else
    begin
      Sorts(FileList,Acol,IsSorted[Acol]);
      IsSorted[Acol] := 0;
    end;
    R4Grid1.Refresh;
end;

//检索按钮
procedure TForm1.SearchClick(Sender: TObject);
begin
  if(FileList.Count<=0) then
  begin
    showmessage('Please extract the path at First.');
    exit;
  end;

  Form2 := TForm2.Create(Self); //将源窗口作为父窗口
  //Form2.Parent := Self;
  try
    Form2.FileList := FileList;
    Form2.OnSearchResult := SearchResultSubmit;
    Form2.position := poMainFormCenter;
    Form2.ShowModal;  //ShowModal 子窗口关闭才可以使用父窗口
  except
    Form2.Free;
    raise;
  end;
end;

//提取路径
procedure TForm1.WithdrawClick(Sender: TObject);
var
  SR : TSearchRec;
  I : Integer;

begin
  FileList.Clear;
  FilePath := find.Text;
  //FilePath := 'C:\code\DelphiProject3';
  if(Length(FilePath)=0) then
  begin
    showmessage('Please enter the directory.');
    Exit;
  end;
  if(not directoryExists(FilePath)) then
  begin
    showmessage('The directory is not found,please re-enter.');
    Exit;
  end;
  LoadFileChild(FileList,FilePath,CheckBox1.Checked);
  R4Grid1.RowCount :=  FileList.Count + 1;
  R4Grid1.Refresh;
end;

//点击决定 进行检索
procedure TForm1.SearchResultSubmit(Sender:TObject;SN :string);
var
  Row :Integer;
begin
  SName := SN;
  Row := FirstSearch(ResultList,FileList,SN);
  if(Row > 0) then
  begin
    R4Grid1.Row := StrToInt(ResultList.Names[0])+1;
    exit;
  end
  else
    Row := SecondSearch(ResultList,FileList,SN);
    if(Row > 0) then
    begin
      R4Grid1.Row := StrToInt(ResultList.Names[0])+1;
      //showmessage(inttostr(R4Grid1.Row ));
      exit;
    end;
    showmessage('検索条件に一致するデータは見つかりません。');
    Exit;
  begin
  end;
end;


end.
